//
//  QLMPhotoInfoTabCell.m
//  Xiaomakuaixiu
//
//  Created by 龚碧容 on 2017/10/18.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "QLMPhotoInfoTabCell.h"
#import "QLMPhotoInfoColCell.h"

@implementation QLMPhotoInfoTabCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self _creatMainUI];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

+ (instancetype)cellWithTableView:(UITableView *)tableView{
    
    static NSString *identifiew=@"QLMPhotoInfoTabCellId";
    QLMPhotoInfoTabCell *cell=[tableView dequeueReusableCellWithIdentifier:identifiew];
    if (cell==nil) {
        cell=[[[NSBundle mainBundle] loadNibNamed:@"QLMPhotoInfoTabCell" owner:nil options:nil] firstObject];
    }
    return cell;
}

- (void)_creatMainUI{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    layout.minimumInteritemSpacing = 0.1;// 垂直方向的间距
    layout.minimumLineSpacing = 0.1; // 水平方向的间距
    
    _photoCollectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, 120) collectionViewLayout:layout];
    _photoCollectionView.delegate=self;
    _photoCollectionView.dataSource=self;
    _photoCollectionView.scrollEnabled=NO;
    _photoCollectionView.showsVerticalScrollIndicator=NO;
    _photoCollectionView.backgroundColor=[UIColor clearColor];
    _photoCollectionView.alwaysBounceVertical = YES;
    _photoCollectionView.collectionViewLayout=layout;
    [_photoCollectionView registerClass:[QLMPhotoInfoColCell class] forCellWithReuseIdentifier:@"QLMPhotoInfoColCellId"];
    [self addSubview:_photoCollectionView];
    
    [_photoCollectionView registerClass:[QLMCartypeCReView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UIReusableViewIdStr"];
}

- (void)setSelectedPhotos:(NSMutableArray *)selectedPhotos{
   _selectedPhotos=selectedPhotos;
    [_photoCollectionView reloadData];
}

- (void)deleteBtnClik:(UIButton *)btn{
    [_delegate deleteImageVAction:btn.tag];
    [_selectedPhotos removeObjectAtIndex:btn.tag];
    [_photoCollectionView reloadData];
}

#pragma mark - UICollectionView datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (_isCanEditable) {
        if(_selectedPhotos.count==5){
            return 5;
        }
        return _selectedPhotos.count+1;
    }
    return _selectedPhotos.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 20, 0, 10);
}

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout*)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((kScreen_width-60)/5, 60);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifiew=@"QLMPhotoInfoColCellId";
    QLMPhotoInfoColCell *photoColCell=[collectionView dequeueReusableCellWithReuseIdentifier:identifiew forIndexPath:indexPath];
    photoColCell.topImage.image = [UIImage imageNamed:@"tianjia"];
    
    if (_selectedPhotos.count!=0) {
        
        if (_selectedPhotos.count-1>=indexPath.row) {
            photoColCell.topImage.image = _selectedPhotos[indexPath.row];
            
            if (_isCanEditable) {
                photoColCell.deleteBtn.hidden = NO;
                photoColCell.deleteBtn.tag = indexPath.row;
                [photoColCell.deleteBtn addTarget:self action:@selector(deleteBtnClik:) forControlEvents:UIControlEventTouchUpInside];
            }else{
                photoColCell.deleteBtn.hidden = YES;
            }

        }else{
            photoColCell.deleteBtn.hidden = YES;
            photoColCell.topImage.image = [UIImage imageNamed:@"tianjia"];
        }
        
        
    }else{
        photoColCell.deleteBtn.hidden = YES;
    }
    
    return photoColCell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if([kind isEqualToString:UICollectionElementKindSectionHeader]){
        _reusableview= [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UIReusableViewIdStr" forIndexPath:indexPath];
        _reusableview.tiplabel.text = @"照片";
        _reusableview.tiplabel.font=[UIFont systemFontOfSize:16];
        _reusableview.tiplabel.textColor=[UIColor darkGrayColor];
        [_reusableview.tiplabel setFrame:CGRectMake(20, 0, kScreen_width-80,34)];
    
        return _reusableview;

    }else if([kind isEqualToString:UICollectionElementKindSectionFooter]) {

    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(kScreen_width-80, 50);
}

#pragma mark -- UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_isCanEditable) {
        if (_selectedPhotos.count!=0) {
            if (indexPath.row<=_selectedPhotos.count-1) {
                [_delegate tapPreViewerAction:indexPath.row];
            }else{
                [_delegate selPhotoImageBtnAction];
            }
        }else{
            [_delegate selPhotoImageBtnAction];
        }
    }else{
        [_delegate tapPreViewerAction:indexPath.row];
    }
}

@end
